package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        //对象属性拷贝
        BeanUtils.copyProperties(dishDTO, dish);
        //设置状态
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.insert(dish);
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                    .id(id)
                    .status(status)
                    .build();
        dishMapper.update(dish);
        if (status == StatusConstant.DISABLE) {
            //根据菜品id查询套餐数据
            ArrayList<Long> ids = new ArrayList<>();
            ids.add(id);
             List<Long> setmealIds = setmealMapper.getSetmealIdsByDishId(ids);
            //根据套餐id查询套餐名称
            if (setmealIds!=null && setmealIds.size()>0){
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                                        .id(setmealId)
                                        .status(StatusConstant.DISABLE)
                                        .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

    @Override
    public DishVO getById(Long id) {
         DishVO dishVO = dishMapper.getById(id);
         return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        //对象属性拷贝
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                    .categoryId(categoryId)
                    .status(StatusConstant.ENABLE)
                    .build();
        return dishMapper.list(dish);
    }
    @Override
    public void deleteById(List<Long> ids) {
        //起售中的菜品不能删除，删除抛异常
        for (Long id : ids) {
           Dish dish = dishMapper.deleteById(id);
            Integer status = dish.getStatus();
            if (status==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //do 被套餐挂关联的菜品不能删除
        //根据菜品id查询套餐id
       List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishId(ids);
        for (Long setmealId : setmealIds) {
            //判断套餐是否起售
            Setmeal setmeal = setmealMapper.getById(setmealId);
            if (setmeal.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        //do 删除菜品后，还要去菜品口味表中删除对应的口味数据
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }
}
